package com.example.himalaya.presenters;

import com.example.himalaya.interfaces.IRecommendPresenter;
import com.example.himalaya.interfaces.IRecommendViewCallBack;
import com.example.himalaya.utils.Constants;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendPresenter implements IRecommendPresenter {
    private static String TAG  = "RecommendPresenter";
    private List<IRecommendViewCallBack> mCallBacks = new ArrayList<>();

    private  RecommendPresenter(){}
    private  static  RecommendPresenter sInstance = null;

    /**
     * 通过单例模式获取对象  获取推荐内容， 接口： 3.10.6 获取猜你喜欢专辑
     * */
    public  static  RecommendPresenter getInstance(){
        if (sInstance == null) {
            synchronized (RecommendPresenter.class){
                sInstance = new RecommendPresenter();
            }
        }
        return sInstance;
    }
    /**
     * 获取推荐内容， 接口： 3.10.6 获取猜你喜欢专辑
     * */
    @Override
    public void getRecommendList() {
        //加载界面
        updateLoading();
        //封装参数
        Map<String, String> map = new HashMap<>();
        //这个参数表示一页数据返回多少条
        map.put(DTransferConstants.LIKE_COUNT, Constants.COUNT_RECOMMEND +"");
        CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                LogUtil.d(TAG," Now Thread name----> " + Thread.currentThread().getName());
                //数据获取成功
                if (gussLikeAlbumList != null) {
                    List<Album> albumList = gussLikeAlbumList.getAlbumList();
                    //获取数据以后，更新UI
                    //upRecommendUI(albumList);
                    handlerRecommendResult(albumList);
                }
            }

            @Override
            public void onError(int i, String s) {
                //数据获取失败
                LogUtil.d(TAG,"error  ---> " + i);
                LogUtil.d(TAG,"error msg ---> " + s);
                handlerError();
            }
        });
    }
    //网络错误
    private void handlerError() {
        if (mCallBacks != null){
            for (IRecommendViewCallBack callBack : mCallBacks){
                callBack.onNetworkError();
            }
        }
    }

    private void handlerRecommendResult(List<Album> albumList) {
        //通知UI更新
        if (albumList != null){
            if (albumList.size() == 0){ //判断传入的内容是否为空，为空则返回空界面
                for (IRecommendViewCallBack callBack : mCallBacks){
                    callBack.onEmpty();
                }
            }else{
                for (IRecommendViewCallBack callBack : mCallBacks){
                    callBack.onRecommendListLoaded(albumList);
                }
            }
        }

    }

    private  void updateLoading(){
        for (IRecommendViewCallBack callBack : mCallBacks){
            callBack.onLoading();
        }
    }

    @Override
    public void pull2RefreshMore() {
    }

    @Override
    public void loadMore() {
    }

    @Override
    public void registerViewCallBack(IRecommendViewCallBack callBack) {
        if (mCallBacks !=null && !mCallBacks.contains(callBack)) {
            mCallBacks.add(callBack);
        }
    }

    @Override
    public void unRegisterViewCallBack(IRecommendViewCallBack callBack) {
        if (mCallBacks!=null) {
            mCallBacks.remove(mCallBacks);
        }
    }
}
